﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_cms_Banner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                LoadQC();
            }

        }
    }
    protected void bt_clickthem(object sender, EventArgs e)
    {

        string ten = txt_tenhang.Text;
        string ghichu = txt_ghichu.Text;
        string sql = " insert into HangBay(Name,GhiChu) values(N'" + ten + "',N'" + ghichu + "') ";
        DataAccess.ExeNonSQL(sql);
        Response.Write("<script>alert('Bạn đã thêm mới thành công')</script>");

    }
    protected void btchange(object sender, CommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        string ten = e.CommandName.ToString();
        if (ten == "X")
        {
            string sql1 = "delete from HangBay where Id='" + id + "'";
            DataAccess.ExeNonSQL(ITNLip.SqlInjectionOK(sql1));
            Response.Write("<script>alert('Bạn vừa xóa thành công 1 bản ghi ! ') </script>");
            LoadQC();
        }
        else
        {
            Button1.Visible = true;
            Session.Add("IdQC",id);
            DataTable dt = new DataTable();
            string sql2 = "select * from HangBay where Id='" + id + "'";
            dt = DataAccess.ExeSQLToDataTable(ITNLip.SqlInjectionOK(sql2));
            if (dt.Rows.Count > 0)
            {
                txt_tenhang.Text = dt.Rows[0][1].ToString();
                txt_ghichu.Text = dt.Rows[0][2].ToString();
            }

        }
    }
    protected void bt_suaclick(object sender, EventArgs e)
    {
        string ten = txt_tenhang.Text;
        string ghichu = txt_ghichu.Text;
        
        string idqc = Session["IdQC"].ToString();
       
            string sql3 = "update HangBay set  Name=N'" + ten + "',GhiChu=N'"+ghichu+"' where Id='" + idqc + "' ";
            DataAccess.ExeNonSQL(sql3);
            LoadQC();
        Response.Write("<script>alert('Bạn sửa dữ liệu thành công')</script>");

    }
    protected void LoadQC()
    {
        string sqlload = "select * from HangBay";
        DataTable dt = new DataTable();
        dt = DataAccess.ExeSQLToDataTable(sqlload);
        if (dt.Rows.Count > 0)
        {
            dtlQC.DataSource = dt.DefaultView;
            dtlQC.DataBind();
        }
    }
    protected void btthaydoi(object sender, GridViewPageEventArgs e)
    {
        dtlQC.PageIndex = e.NewPageIndex;
        this.LoadQC();
    }
}